var express = require('express');
var router = express.Router();
var User = require('../models/user');
/* GET home page. */
router.get('/', function(req, res, next) {
  res.sendfile('./public/views/index.html');
});

router.post('/login',function(req,res){
      var username = req.body.name;
      var password = req.body.password;
      if (!username||!password) {
        res.json({loginMessage:'请输入账号和密码！'});
      } else {
        User.findOne({'name':username},function(err,user){
        if (err)
           res.json({loginMessage:'账号不存在！'});
         // res.cookie("user",user);
         // req.session.user = user;
         // console.log(req.session.user);
         if (user) {
              if(password != user.password) {
                res.json({loginMessage:'账号和密码不匹配'});
              } else {
                res.cookie("user",user);
                req.session.user = user;
                console.log(req.session);
                res.json({user:user});
              }
         } else {
            res.json({loginMessage:'账号不存在！'});
         } 
       });
    }
});

router.post("/register",function(req, res) { 
      var user = new User();      // create a new instance of the Bear model
      user.name = req.body.name; 
      user.password = req.body.password;
      user.type = req.body.type;
     // mg.date = new Date();
      // save the bear and check for errors
      user.save(function(err) {
          if (err)
              res.json({message: 'failed',success: false});
        //  req.session.user = user; //why session here is null
      res.cookie("user",user);
      req.session.user = user;
          res.json({ message: 'user created!',success: true});
      });   
});

router.get('/session',function(req,res){
  res.json(req.cookies);
});

router.get('/logout',function(req,res){
     res.cookie("user",null);
     req.session.user = null;
     res.json({success: true});
});

router.get('/api', function(req, res, next) {
  res.json({ message: 'hooray! welcome to our api!' });  
});
/*router.get('/messages', function(req, res, next) {
  res.sendfile('./public/views/messages.html');
});
router.get('/tallybook', function(req, res, next) {
  res.sendfile('./public/views/tallybook.html');
});*/
router.get('/contact', function(req, res, next) {
  res.sendfile('./public/views/contact.html');
});

router.get('/messages', function(req, res, next) {
  if(req.cookies&&req.cookies.user){
		res.sendfile('./public/views/messages.html');
  }else{
		res.redirect('/login');
  }
});

router.get('/tallybook', function(req, res, next) {
	if(req.cookies&&req.cookies.user){
		res.sendfile('./public/views/tallybook.html');
  }else{
		res.redirect('/login');
  }
 
});
router.get('/register',function(req, res, next) {
  res.sendfile('./public/views/register.html');
});
router.get('/login',function(req, res, next) {
  res.sendfile('./public/views/login.html');
});

router.get('/insert', function(req, res, next) {
  if(req.cookies&&req.cookies.user&&req.cookies.user.name == 'Admin'){
  	 res.sendfile('./public/views/insert.html');
  }else{
  	res.redirect('/');
  }
});
module.exports = router;
